/*
**************************************************************************************
*Myriad Engine                                                                       *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
package leola.lang;

import java.io.File;
import java.io.RandomAccessFile;

import leola.frontend.Leola;
import leola.frontend.LeolaLibrary;
import leola.lang.io.Buffer;
import leola.types.LeoNativeClass;

/**
 * @author Tony
 *
 */
public class IOLeolaLibrary implements LeolaLibrary {

	/* (non-Javadoc)
	 * @see leola.frontend.LeolaLibrary#init(leola.frontend.Leola)
	 */
	@Override
	public void init(Leola leola) throws Exception {
		leola.getInterpreter().loadClass(IOLeolaLibrary.class);		
	}

	/**
	 * A {@link LeolaFile}
	 * 
	 * @author Tony
	 *
	 */
	public static class LeolaFile {
		
		/**
		 * file
		 */
		public RandomAccessFile raf;
		
		public LeolaFile(RandomAccessFile raf) {
			this.raf = raf;
		}
	}
	
	public static final Buffer newBuffer(int capacity) {
		return new Buffer(capacity);
	}
	
	public static final LeoNativeClass fopen(String filename) throws Exception {
		RandomAccessFile raf = new RandomAccessFile(new File(filename), "rw");
		return new LeoNativeClass(new LeolaFile(raf));
	}
	
	/**
	 * Reads in and stores into the buffer.
	 * 
	 * @param file
	 * @param buffer
	 * @return
	 * @throws Exception
	 */
	public static final int fread(LeolaFile file, Buffer buffer) throws Exception {		
		int bytesRead = file.raf.read(buffer.getArray());		
		buffer.setLimit(bytesRead);
		return bytesRead;
	}
	
	/**
	 * Writes out bytes.
	 * 
	 * @param file
	 * @param buffer
	 * @throws Exception
	 */
	public static final void fwrite(LeolaFile file, Buffer buffer) throws Exception {
		file.raf.write(buffer.getArray(), buffer.position(), buffer.remaining());
	}
	
	/**
	 * Seeks into a file.
	 * 
	 * @param pFile
	 * @param pos
	 * @throws Exception
	 */
	public static final void fseek(LeolaFile file, long pos) throws Exception {
		file.raf.seek(pos);
	}
	
	/**
	 * Closes the file.
	 * 
	 * @param file
	 * @throws Exception
	 */
	public static final void fclose(LeolaFile file) throws Exception {
		file.raf.close();
	}
}
